package labox.innovation.gameserver.datatables;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import javolution.util.FastList;
import javolution.util.FastMap;
import labox.innovation.config.Config;
import labox.innovation.gameserver.templates.chars.CharTemplate;
import labox.innovation.gameserver.templates.chars.NpcTemplate;

/**
 * NPC角色模板管理表，装载和维护所有的NPC角色模板
 * 
 * @version $Revision: 1.8.2.6.2.9 $ $Date: 2005/04/06 16:13:25 $
 */
public final class NpcTemplateTable {

	private static final Logger _log = LoggerFactory.getLogger(NpcTemplateTable.class.getName());

	protected Map<Integer, NpcTemplate> _npc = new FastMap<Integer, NpcTemplate>();

	public static NpcTemplateTable getInstance() {
		return SingletonHolder._instance;
	}

	private NpcTemplateTable() {
	}

	public final void fromXML() {
		try {
			File f = new File(Config.DATAPACK_ROOT, "/Scene/npcTemplate.xml");
			FileInputStream fin = new FileInputStream(f);
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(fin);
			Element root;
			root = document.getDocumentElement();
			if (root.getTagName().compareTo("NpcTemplates") != 0) {
				throw new Exception("Not a NpcTemplates file!");
			}

			NodeList nl = root.getElementsByTagName("ActorClass");
			Element el;
			for (int i = -1; ++i < nl.getLength();) {
				el = (Element) nl.item(i);
				NpcTemplate ct = new NpcTemplate(el);
				getInstance()._npc.put(ct._id, ct);
			}
			fin.close();
		} catch (Exception ex) {
			_log.error("CharTemplateTable: Error loading data.", Config.SERVER_ID, ex);
		}
	}

	public void replaceTemplate(NpcTemplate npc) {
		_npc.put(npc._id, npc);
	}

	public NpcTemplate getTemplate(int id) {
		return (NpcTemplate) _npc.get(id);
	}

	public NpcTemplate getTemplateByName(String name) {
		for (CharTemplate npcTemplate : _npc.values()) {
			if (npcTemplate instanceof NpcTemplate) {
				NpcTemplate temp = (NpcTemplate) npcTemplate;
				if (temp.name.equalsIgnoreCase(name))
					return temp;
			}
		}
		return null;
	}

	public NpcTemplate[] getAllOfLevel(int lvl) {
		List<NpcTemplate> list = new FastList<NpcTemplate>();

		for (CharTemplate t : _npc.values()) {
			if (t instanceof NpcTemplate) {
				NpcTemplate temp = (NpcTemplate) t;
				if (temp.level == lvl)
					list.add(temp);
			}
		}

		return list.toArray(new NpcTemplate[list.size()]);
	}

	public NpcTemplate[] getAllMonstersOfLevel(int lvl) {
		List<NpcTemplate> list = new FastList<NpcTemplate>();
		for (CharTemplate t : _npc.values()) {
			if (t instanceof NpcTemplate) {
				NpcTemplate temp = (NpcTemplate) t;
				if (temp.level == lvl && "L2Monster".equals(temp.type))
					list.add(temp);
			}
		}
		return list.toArray(new NpcTemplate[list.size()]);
	}

	public NpcTemplate[] getAllNpcStartingWith(String letter) {
		List<NpcTemplate> list = new FastList<NpcTemplate>();
		for (CharTemplate t : _npc.values()) {
			if (t instanceof NpcTemplate) {
				NpcTemplate temp = (NpcTemplate) t;
				if (temp.name.startsWith(letter) && "L2Npc".equals(temp.type))
					list.add(temp);
			}
		}
		return list.toArray(new NpcTemplate[list.size()]);
	}

	/**
	 * @param classType
	 * @return
	 */
	public Set<Integer> getAllNpcOfClassType(String classType) {
		return null;
	}

	/**
	 * @param class1
	 * @return
	 */
	public Set<Integer> getAllNpcOfL2jClass(Class<?> clazz) {
		return null;
	}

	/**
	 * @param aiType
	 * @return
	 */
	public Set<Integer> getAllNpcOfAiType(String aiType) {
		return null;
	}

	@SuppressWarnings("synthetic-access")
	private static class SingletonHolder {
		protected static final NpcTemplateTable _instance = new NpcTemplateTable();
	}
}
